<?
$now = date('Y-m-d',time());

$user = User::authenticate($_SESSION['account']);
if (!$user)
{
	// Create user if not exist
	$userTest = new User();
	// Create user if not exist
	$user = $userTest->isExistEmail($txt_email);
	if (!$user)
	{
		$sha_password = sha1($txt_email);
		$username = $txt_email;
		$password = md5($sha_password.$txt_email);
		$user = new User();
		$user->setusername($username);
		$user->setpassword($password);
		$user->setname($txt_fullname);
		$user->setemail($txt_email);
		$user->setphone_number($txt_tel);
		$user->setfax_number($txt_fax);
		$user->setaddress($txt_address);
		$user->setnationality($sbo_language);
		$user->setauthoriy(2);
		$user->setstatus(0);
		$user->setcreated_at($now);
		$user->setupdated_at($now);
		
		$user->insert();
		
		User::sendActiveEmail($txt_email, $sha_password);
	} 
}

// CREATE INVOICE
$invoice = new Invoice();
$invoice->settype($rad_contact_type);
$invoice->setuser_id($user->getid());
$invoice->setpayment_method($rad_payment_method);
$invoice->setcompany_name($txt_company_name);
$invoice->settax_code($txt_tax_code);
$invoice->setstatus('CREATE');
$invoice->setcreated_at($now);

$invoice->create();
$transfer_types = array(
	0 => 'Máy bay',
	1 => 'Tàu lửa',
	2 => 'Ôtô',
	3 => 'Tàu thủy'
);
$transfer = new Transfer();
$transfer->setuser_id($user->getid());
$transfer->setdeparture($sbo_location);
$transfer->setdestination($sbo_location_destination);
$transfer->setdeparture_time($txt_departure_time);
$transfer->setreturn_time($txt_return_time);
$transfer->setactive(-1);
$transfer->settype($sbo_transfer_type);
$transfer->insert();

// CREATE ORDER FOR EACH PASSENGERS
$order = new ClientOrder();
$order->setinvoice_id($invoice->getid());
$order->settransfer_id($transfer_id);
$order->setcreated_at($now);
		
$order->insert();

$ages = array(
	'adult' => "ADULT",
	'child' => "CHILD",
	'infant' => "INFANT"
);				

// CREATE PASSENGERS
foreach ($ages as $key => $value)
{
	for ($i=1; $i<=$_POST['sbo_number_'.$key]; $i++)
	{
		$passenger_name = $_POST['txt_guest_'.$key.'_fullname_'.$i];
		$passenger_email = $_POST['txt_guest_'.$key.'_email_'.$i];
		$passenger_phone_number = $_POST['txt_guest_'.$key.'_phone_number_'.$i];
		$passenger_birthday = lg_date::dmy2ymd($_POST['txt_guest_'.$key.'_birthday_'.$i]);
		$passenger_identify_number = $_POST['txt_guest_'.$key.'_identify_number_'.$i];
		
		if ($passenger_name == 'Họ và tên')
			$passenger_name = '';
		if ($passenger_email == 'Email')
			$passenger_email = '';
		if ($passenger_phone_number == 'Số điện thoại')
			$passenger_phone_number = '';
		if ($passenger_birthday == 'Ngày sinh')
			$passenger_birthday= '';
		if ($passenger_identify_number == 'CMND/Passport')
			$passenger_identify_number = '';
		
		$customer = new Customer();
		$customer->setname($passenger_name);
		$customer->setphone_number($passenger_phone_number);
		$customer->setbirthday($passenger_birthday);
		$customer->setidentify_number($passenger_identify_number);
		$customer->setage_type($value);
		$customer->setactive(1);
		$customer->setorder_id($order->getid());
		$customer->setcreated_at(date("Y-m-d"));
		$customer->insert();
	}
}

?>